﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AngleSharp.Parser.Html;

namespace Spider.Spider
{
    public class StockNameSpider
    {
        public static Dictionary<String, String> GetStockName()
        {
            var dictionary = new Dictionary<String, String>();
            HttpWebRequest requestMain = (HttpWebRequest) WebRequest.Create("http://quote.eastmoney.com/stocklist.html");
            requestMain.Method = WebRequestMethods.Http.Get;
            requestMain.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            requestMain.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
            requestMain.Headers.Add("Origin:http://quote.eastmoney.com");
            requestMain.Headers.Add("Pragma:no-cache");
            requestMain.Headers.Add("X-Requested-With:XMLHttpRequest");
            requestMain.UserAgent =
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
            HttpWebResponse webResponse = (HttpWebResponse) requestMain.GetResponse();
            var responseStream = webResponse.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(responseStream, Encoding.GetEncoding("GB2312"));
            string retString = myStreamReader.ReadToEnd();

            var parser = new HtmlParser();
            var htmlDocument = parser.Parse(retString);

            var quotesearchElement = htmlDocument.QuerySelectorAll("div").First(m => "quotesearch".Equals(m.Id));

            Encoding gb2312 = Encoding.GetEncoding("GB2312");

            var uls = quotesearchElement.QuerySelectorAll("ul");
            foreach (var ul in uls)
            {
                var li = ul.QuerySelectorAll("a");
                var list = li.Where(m => "_blank".Equals(m.GetAttribute("target"))).Select(m=>m.InnerHtml);
                foreach (var htm in list)
                {
                    var match = Regex.Match(htm, "(.*?)\\(([0-9]{6,6})\\)");
                    if (match.Success)
                    {
                        var name = Gb2312ToUtf8(match.Groups[1].Value);
                        var code = match.Groups[2].Value.ToString();
                        dictionary.Add(code, name);
                    }
                }
            }

            return dictionary;
        }

        private static String Gb2312ToUtf8(string str)
        {
            try
            {
                Encoding utf8 = Encoding.UTF8;
                Encoding gb2312 = Encoding.GetEncoding("GB2312");
                byte[] unicodeBytes = gb2312.GetBytes(str);
                byte[] asciiBytes = Encoding.Convert(gb2312, utf8, unicodeBytes);
                char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
                utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
                string result = new string(asciiChars);
                return result;
            }
            catch
            {
                return "";
            }
        }
    }
}